<?php
class App_Module_Admin_Component_Media_Ctrl_Http extends Mozg_Controller_Instance
{
    protected $_page = 1;
    protected $_media;

    protected function GetMediaFromQueryId()
    {
		$id = (int)Mozg::Request('http')->get('id');
		if ($id)
		{
			return Mozg_Mapper::Get('Media')->GetOne(array('id' => $id));
		}

		return false;
    }
    
    protected function GetQueryPage()
    {
    	if (Mozg::Request('http')->hasQuery('page'))
    	{
			$this->_page = (int)Mozg::Request('http')->getQuery('page');
			if ($this->_page != Mozg::Request('http')->getCookie('page'))
			{
				$link = Mozg_Router::Get('http')->Create(array('module' => 'admin', 'ctrl' => $this->GetName()));
				Mozg::Request('http')->SetCookie('page', $this->_page, 0, $link);
			}
    	}
    	else
    	{
    		$this->_page = (int)Mozg::Request('http')->getCookie('page');
    	}

    	return $this->_page;
    }

    protected function GetFilter($prefixName = 'media')
    {
		$filter = Mozg_Mapper::Get('Media')->GetFilter();
		$session = Mozg_Mapper::Get('Session')->GetEntity('admin', 'mediafilter' .$prefixName);

		if (Mozg::Request('http')->hasQuery('resetfilter'))
		{
			Mozg_Mapper::Get('Session')->Delete($session);
			return $filter;
		}
		else if (Mozg::Request('http')->hasQuery('mediafilter'))
		{
	   		$this->GetTpl('Filterform')->SetPostData($filter, (array)Mozg::Request('http')->getQuery('mediafilter'));
	   		
	   		$session->ImportData($filter->ExportData());
	   		Mozg_Mapper::Get('Session')->Save($session);
		}
		else
		{
			$filter->ImportData($session->ExportData());
		}
    	
    	return $filter;
    }

    public function OutMediaFilesBlock($baseUrlParams, $params = array())
    {
        $filter = $this->GetFilter('mediablock');
        $page = (int)Mozg::Request('http')->getQuery('media_page');
    	
        return $this->GetTpl('BlockList')->GetList($this->GetName(), $filter, $baseUrlParams, $page, $params);
    }
}